import java.util.Scanner;

public class 机器人跳跃问题 {
    //https://www.nowcoder.com/practice/7037a3d57bbd4336856b8e16a9cafd71
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        int max = 0;
        for(int i = 0;i < n;i++){
            arr[i] = in.nextInt();
            max = Math.max(max,arr[i]);
        }
        int l = 1;
        int r = max;
        //二分遍历答案
        while(l < r){
            int mid = (l + r) >> 1;
            if(f(arr,mid,max) == -1){
                l = mid + 1;
            }else{
                r = mid;
            }
        }
        System.out.println(r);
    }

    public static int f(int[] arr,int energy,int max){
        for(int i = 0;i < arr.length;i++){
            //如果大于最大值的话, 就直接返回
            if(energy >= max){
                return 1;
            }
            //判断+-
            if(arr[i] > energy){
                energy -= arr[i] - energy;
            }else{
                energy += energy - arr[i];
            }
            //判断是否小于0
            if(energy < 0){
                return -1;
            }
        }
        return 1;
    }
}
